package com.yizhubao.app.activitys.home;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.viewpager.widget.ViewPager;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.yizhubao.app.R;
import com.yizhubao.app.activitys.WebActivity;
import com.yizhubao.app.base.BaseActivityToolBar;
import com.yizhubao.app.beans.NormalSwipeBean;
import com.yizhubao.app.holders.NormalSwipeHolder;
import com.yizhubao.app.utils.Constants;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import h.framework.pacy.utils.LogUtil;
import h.framework.pacy.utils.ScreenUtil;

/**
 * 创建人: PACY
 * 创建时间: 6/4/21 10:25 AM
 * 邮箱: pengwwe457621023@qq.com
 * 类描述: 专业遗嘱轮播
 */
public class ProdSwipeActivity extends BaseActivityToolBar {
    //轮播
    @BindView(R.id.banner)
    ConvenientBanner banner;

    //步骤文本
    @BindView(R.id.buzou_tv)
    TextView buzou_tv;

    //指示器
    @BindView(R.id.layout_group)
    LinearLayout layout_group;
    ImageView[] imageViews;


    //轮播数据
    List<NormalSwipeBean> datas;

    @Override
    protected int setContentLayout() {
        return R.layout.activity_prod_swipe;
    }

    @Override
    protected void initView() {
        setTitleOnLeftIcon(R.drawable.icon_back,"");
        initBanner();
    }

    @Override
    protected void initData() {

    }

    /**
     * 初始化轮播
     */
    private void initBanner(){
        datas=new ArrayList<>();
        datas.add(new NormalSwipeBean("联系专家，一对一指导","共820名全国顶级专家，全程在线，专业指导。很方便的，制作合法遗嘱",R.drawable.pic_shouye2_1));
        datas.add(new NormalSwipeBean("加密永久保存","军工级电子证据加密，仅自己可见，永不丢失",R.drawable.pic_shouye1_3));
        datas.add(new NormalSwipeBean("健康实时关注","结合公安部数据，多种方式，主动关注你的健康状态",R.drawable.pic_shouye1_4));
        datas.add(new NormalSwipeBean("线下专人服务","若发生意外，平台将第一时间，把你的遗嘱移交给联系人。并全程辅导料理后事！",R.drawable.pic_shouye1_5));

        banner.setPages((CBViewHolderCreator<NormalSwipeHolder>) () -> new NormalSwipeHolder(), datas)
//                .setPageIndicator(new int[]{R.drawable.maodian_normal, R.drawable.maodian_selected})
                .setOnItemClickListener(position -> {
//                        sendBannerClick(imgs.get(position));

                }).setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                LogUtil.i("position=="+position);
                buzou_tv.setText("步骤"+(position+1)+"/4");
                selectCircle(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        banner.startTurning(10*1000);
        setCircleTab();
    }


    @OnClick(value = {R.id.looking_tv})
    public void click(View v){
        if(v.getId()==R.id.looking_tv){
            Bundle bundle=new Bundle();
            bundle.putString("title","联系律师");
            bundle.putString("url", Constants.LIANXILVSHI);
            startActivity(ProdSwipeActivity.this, WebActivity.class,bundle);
        }
    }

    /**
     * 初始化小圆点
     */
    private void setCircleTab() {
        layout_group.removeAllViews();
        imageViews = new ImageView[datas.size()];
        for (int i = 0; i < datas.size(); i++) {
            ImageView imageView = new ImageView(ProdSwipeActivity.this);
            LinearLayout.LayoutParams params;
            imageViews[i] = imageView;
            if (i == 0) {
                imageViews[i].setImageResource(R.drawable.maodian_selected);
               params=new LinearLayout.LayoutParams(ScreenUtil.dp2px(12),ScreenUtil.dp2px(4));
                params.leftMargin=ScreenUtil.dp2px(3);
                imageViews[i].setLayoutParams(params);
            } else {
                imageViews[i].setImageResource(R.drawable.maodian_normal);
                params=new LinearLayout.LayoutParams(ScreenUtil.dp2px(4),ScreenUtil.dp2px(4));
                params.leftMargin=ScreenUtil.dp2px(3);
                imageViews[i].setLayoutParams(params);
            }
            layout_group.addView(imageViews[i]);
        }
    }

    /**
     * 选项卡切换时,切换小圆点
     *
     * @param postion
     */
    private void selectCircle(int postion) {
        for (int i = 0; i < imageViews.length; i++) {
            LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(ScreenUtil.dp2px(12),ScreenUtil.dp2px(4));
            params.leftMargin=ScreenUtil.dp2px(3);
            imageViews[postion].setImageResource(R.drawable.maodian_selected);
            imageViews[postion].setLayoutParams(params);
            if (postion != i) {
                LinearLayout.LayoutParams params2=new LinearLayout.LayoutParams(ScreenUtil.dp2px(4),ScreenUtil.dp2px(4));
                params2.leftMargin=ScreenUtil.dp2px(3);
                imageViews[i].setImageResource(R.drawable.maodian_normal);
                imageViews[i].setLayoutParams(params2);
            }
        }
    }
}